﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Reflection;

namespace HML.Design
{
    /// <summary>
    ///  集合属性编辑器(显示Text文本、显示属性详细描述信息)
    /// </summary>
    public class CollectionTextHelpEditor : CollectionEditor
    {
        public CollectionTextHelpEditor(Type type) : base(type)
        {

        }

        protected override string GetDisplayText(object value)
        {
            if (value == null)
            {
                return string.Empty;
            }

            PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(value)["Text"];
            if (propertyDescriptor != null && propertyDescriptor.PropertyType == typeof(string))
            {
                string str = (string)propertyDescriptor.GetValue(value);
                if (str != null && str.Length > 0)
                {
                    return str;
                }
            }
            PropertyDescriptor defaultProperty = TypeDescriptor.GetDefaultProperty(this.CollectionType);
            if (defaultProperty != null && defaultProperty.PropertyType == typeof(string))
            {
                string str = (string)defaultProperty.GetValue(value);
                if (str != null && str.Length > 0)
                {
                    return str;
                }
            }
            string str1 = TypeDescriptor.GetConverter(value).ConvertToString(value);
            if (str1 == null || str1.Length == 0)
            {
                str1 = value.GetType().Name;
            }
            return str1;
        }

        protected override CollectionForm CreateCollectionForm()
        {
            CollectionForm frm = base.CreateCollectionForm();
            FieldInfo fileinfo = frm.GetType().GetField("propertyBrowser", BindingFlags.NonPublic | BindingFlags.Instance);
            if (fileinfo != null)
            {
                (fileinfo.GetValue(frm) as System.Windows.Forms.PropertyGrid).HelpVisible = true;
            }
            frm.Width = 580;
            frm.Height = 420;
            return frm;
        }
    }
}
